
package com.xinzhi.dao;

import com.xinzhi.entity.User;

import java.sql.*;

/**
 * 对用户修改类
 */
public class UserDao {
    /**
     * 登录类
     */
    public int login(String username, String password) {
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int count = -1;
        try {
            conn = BaseDao.getConn();
            //定义sql语句框架
            String sql = "select * from user where  username = ? and password = ?";
            //sql语句进行预编译
            stat = conn.prepareStatement(sql);
            //对sql语句进行赋值
            stat.setString(1, username);
            stat.setString(2, password);
            //调用数据库
            rs = stat.executeQuery();
            if (rs.next()) {
                count = 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(rs, stat, conn);
        }
        return count;
    }

    /**
     * 注册类
     */
    public int enroll(User us) {
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int count = -1;
        try {
            conn = BaseDao.getConn();
            //定义sql语句框架
            String sql = "insert into user values (?,?,0,?)";
            //sql语句进行预编译
            stat = conn.prepareStatement(sql);
            //对sql语句进行赋值
            stat.setString(1, us.getUsername());
            stat.setString(2, us.getPassword());
            stat.setString(3, us.getAddress());
            //调用数据库
            count = stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(rs, stat, conn);
        }
        return count;
    }

    /**
     * 注册判断
     *
     * @param username
     * @return
     */
    public int enroll_judge(String username) {
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int count = -1;
        try {
            conn = BaseDao.getConn();
            String sql = "select * from user where username = ? ";
            stat = conn.prepareStatement(sql);
            stat.setString(1, username);
            rs = stat.executeQuery();
            if (rs.next()) {
                count = 1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeAll(rs, stat, conn);
        }
        return count;
    }

    /**
     * 查询余额
     */
    public User InquireBalance(String username) {
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        User ui = new User();
        try {
            conn = BaseDao.getConn();
            //创建sql语句的Statement对象
            String sql = "select * from user where username = ?";
            //sql预加载
            stat = conn.prepareStatement(sql);
            stat.setString(1, username);
            rs = stat.executeQuery();
            if (rs.next()) {
                ui.setUsername(rs.getString("username"));
                ui.setPassword(rs.getNString("password"));
                ui.setAddress(rs.getString("address"));
                ui.setBalance(rs.getInt("balance"));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeAll(rs, stat, conn);
        }
        return ui;
    }

    /**
     * 充值修改余额
     */
    public int InquireBalance(User user, String username) {
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int count = -1;
        try {
            conn = BaseDao.getConn();
            //创建sql语句
            String sql = "update user set balance=? where username=?;";
            //第一个？要改的值
            //第二个？要改的行
            //sql预加载
            stat = conn.prepareStatement(sql);
            stat.setDouble(1, user.getBalance());
            stat.setString(2, username);
            count = stat.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeAll(rs, stat, conn);
        }
        return count;
    }

    /**
     * 付款后修改余额
     */
    public int Inquire(Double balance, String username) {
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int count = -1;
        try {
            conn = BaseDao.getConn();
            //创建sql语句
            String sql = "update user set balance=? where username=?;";
            //第一个？要改的值
            //第二个？要改的行
            //sql预加载
            stat = conn.prepareStatement(sql);
            stat.setDouble(1, balance);
            stat.setString(2, username);
            count = stat.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeAll(rs, stat, conn);
        }
        return count;
    }

    /**
     * 修改密码
     *
     * @param user
     * @param username
     * @return
     */
    public int password(User user, String username) {
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int count = -1;
        try {
            conn = BaseDao.getConn();
            //创建sql语句
            String sql = "update user set password =? where username = ?";
            //第一个？要改的值
            //第二个？要改的行
            //sql预加载
            stat = conn.prepareStatement(sql);
            stat.setString(1, user.getPassword());
            stat.setString(2, username);
            count = stat.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeAll(rs, stat, conn);
        }
        return count;

    }

    /**
     * 修改地址
     *
     * @param user
     * @param username
     * @return
     */
    public int address(User user, String username) {
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int count = -1;
        try {
            conn = BaseDao.getConn();
            //创建sql语句
            String sql = "update user set address=? where username=?";
            //sql预加载
            stat = conn.prepareStatement(sql);
            stat.setString(1, user.getAddress());
            stat.setString(2, username);
            count = stat.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeAll(rs, stat, conn);
        }
        return count;
    }


}
